package pe.com.hermes.hermetic.web.controller.general;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import pe.com.hermes.hermetic.beans.ClienteBean;
import pe.com.hermes.hermetic.beans.TiposBean;
import pe.com.hermes.hermetic.negocio.service.HermeticFacade;
import pe.com.hermes.hermetic.negocio.service.TablService;
import pe.com.hermes.hermetic.negocio.ultil.ConstantesHermetic;
import pe.com.hermes.hermetic.negocio.ultil.Util;
import pe.com.hermes.hermetic.web.forms.SelecClienteForm;
import pe.com.hermes.hermetic.web.controller.base.IController;
import pe.com.hermes.hermetic.web.forms.SelecTipoPedidoForm;

@Controller
@RequestMapping("/selecTipoPedido.html") 
@SessionAttributes(value={"listTipoIngresoPedi"})


public class SeleccionarTipoPedidoController implements IController{
  protected final static Logger logger = Logger.getLogger(SeleccionarTipoPedidoController.class);
  @Autowired
  private HermeticFacade servicioHermetic;
  @Autowired
  private TablService tablService;
  
  @RequestMapping(method={RequestMethod.GET})  
  protected String showForm(ModelMap model,HttpServletRequest request)throws Exception{
      try {
          logger.info(this.getClass().toString()+" "+"metodo: GET");
          
          SelecTipoPedidoForm form = new SelecTipoPedidoForm();
          
          List listTipoIngresoPedi = new ArrayList();  
          
          listTipoIngresoPedi = servicioHermetic.buscarTipoIngresoPedido("");
                  
          //asigno los valores de las cuentas seleccionadas en session
          String[] chksTipPedi = (String[])request.getSession(false).getAttribute("chksTipPedi");
          listTipoIngresoPedi = Util.setChksSession(chksTipPedi, listTipoIngresoPedi);
          form.setIndTipo("0");
          form.setListTipoIngresoPedi(listTipoIngresoPedi);
          request.getSession(false).setAttribute("listTipoIngresoPedi",listTipoIngresoPedi);  
          model.put("listTipoIngresoPedi",listTipoIngresoPedi);
          model.put(FORM_KEY,form);
          return FORM_SELECT_TIP_PEDI;
      }
      catch (Exception e) {
          logger.error(e);
          return FORM_ERROR_POPUP_VIEW;
      }
  }
  
  @RequestMapping(params={"accion=selecTipoPedidoTv"}) 
  protected String selecTipoPedidoTv(ModelMap model,HttpServletRequest request)throws Exception{
      try {
          
          SelecTipoPedidoForm form = new SelecTipoPedidoForm();
          
          List listTipoIngresoPedi = new ArrayList();  
          
          listTipoIngresoPedi = tablService.getTiposDinamico(ConstantesHermetic.TIPO_INGR_PEDI,ConstantesHermetic.IND_TIPO_INGR_TV, ConstantesHermetic.CAMP_TIPO_INGR_TV);
                  
          //asigno los valores de las cuentas seleccionadas en session
          String[] chksTipPedi = (String[])request.getSession(false).getAttribute("chksTipPedi");
          listTipoIngresoPedi = Util.setChksSession(chksTipPedi, listTipoIngresoPedi);
          form.setIndTipo("1");
          form.setListTipoIngresoPedi(listTipoIngresoPedi);
          request.getSession(false).setAttribute("listTipoIngresoPedi",listTipoIngresoPedi);  
          model.put("listTipoIngresoPedi",listTipoIngresoPedi);
          model.put(FORM_KEY,form);
          return FORM_SELECT_TIP_PEDI;
      }
      catch (Exception e) {
          logger.error(e);
          return FORM_ERROR_POPUP_VIEW;
      }
  }
  
  @RequestMapping(params={"accion=selectTodos","codTodos"}) 
  protected String selectTodos(@RequestParam("codTodos") boolean codTodos ,@ModelAttribute(FORM_KEY) SelecTipoPedidoForm form,ModelMap model,HttpServletRequest request)throws Exception{
      logger.info(this.getClass().toString()+" "+"metodo: GET");
      
      //Asigno el check a todos los elementos de la lista
       
      List listTipoIngresoPedi = (List)request.getSession(false).getAttribute("listTipoIngresoPedi"); 
      
      listTipoIngresoPedi = Util.setChksTodos(codTodos, listTipoIngresoPedi);        
      form.setListTipoIngresoPedi(listTipoIngresoPedi);
      //Subo a session los todos los Checks Cuenta
      if (codTodos) {
          String[] chksTipPedi = Util.listToStringArray(listTipoIngresoPedi);
          request.getSession(false).setAttribute("chksTipPedi",chksTipPedi);       
      }else {
          request.getSession(false).setAttribute("chksTipPedi",null);        
      }
      
      request.getSession(false).setAttribute("listTipoIngresoPedi",listTipoIngresoPedi);
      model.put("listTipoIngresoPedi",listTipoIngresoPedi);
      //request.getSession(false).setAttribute("chksTipPedi",null);
      model.put(FORM_KEY,form);
      return FORM_SELECT_TIP_PEDI;
  }

  @RequestMapping(params={"accion=searchDesTipoPedi"}) 
  protected @ResponseBody String[] searchDesTipoPedi(HttpServletRequest request)throws Exception{        
      logger.info(this.getClass().toString()+" "+"metodo: searchDesTipoPedi");        
      List listModulos = (List)request.getSession(false).getAttribute("listTipoIngresoPedi");
      String[] arrayModulos = new String[listModulos.size()];
      for(int i=0;i<listModulos.size();i++){
          arrayModulos[i] = ((TiposBean)listModulos.get(i)).getDescripcion();
      }
      return arrayModulos;
  }
  
  @RequestMapping(params={"accion=buscarTipoPedi","desTipoIngreso"}) 
  protected String buscarTipoPedi(@RequestParam("desTipoIngreso") String desTipoIngreso , @ModelAttribute(FORM_KEY) SelecTipoPedidoForm form, ModelMap model,HttpServletRequest request)throws Exception{
      logger.info(this.getClass().toString()+" "+"metodo: GET");
      String[] chksTipPedi = (String[])request.getSession(false).getAttribute("chksTipPedi");
      
      List listTipoIngresoPedi = new ArrayList();        
              
      listTipoIngresoPedi = servicioHermetic.buscarTipoIngresoPedido(desTipoIngreso);
      listTipoIngresoPedi = Util.setChksSession(chksTipPedi,listTipoIngresoPedi);
      //form.setListClientes(listTipoIngresoPedi);
      if(desTipoIngreso.equals(""))
          request.getSession(false).setAttribute("listTipoIngresoPedi",listTipoIngresoPedi);
      model.put("listTipoIngresoPedi",listTipoIngresoPedi);
      model.put(FORM_KEY,form);
      return FORM_SELECT_TIP_PEDI;
  }
  
  @RequestMapping(params={"accion=buscarTipoPediTv","desTipoIngreso"}) 
  protected String buscarTipoPediTv(@RequestParam("desTipoIngreso") String desTipoIngreso ,@ModelAttribute(FORM_KEY) SelecTipoPedidoForm form,ModelMap model,HttpServletRequest request)throws Exception{
      logger.info(this.getClass().toString()+" "+"metodo: GET");
      String[] chksTipPedi = (String[])request.getSession(false).getAttribute("chksTipPedi");
      
      List listTipoIngresoPedi = new ArrayList();        
      List listTipoIngresoPediAux = new ArrayList();                
      listTipoIngresoPediAux = tablService.getTiposDinamico(ConstantesHermetic.TIPO_INGR_PEDI,ConstantesHermetic.IND_TIPO_INGR_TV, ConstantesHermetic.CAMP_TIPO_INGR_TV);
      for (TiposBean tipo : (List<TiposBean>)listTipoIngresoPediAux){
          if (tipo.getDescripcion().contains(desTipoIngreso.toUpperCase())){
            listTipoIngresoPedi.add(tipo);
          }
      }
      listTipoIngresoPedi = Util.setChksSession(chksTipPedi,listTipoIngresoPedi);
      //form.setListClientes(listTipoIngresoPedi);
      if(desTipoIngreso.equals(""))
          request.getSession(false).setAttribute("listTipoIngresoPedi",listTipoIngresoPedi);
      model.put("listTipoIngresoPedi",listTipoIngresoPedi);
      model.put(FORM_KEY,form);
      return FORM_SELECT_TIP_PEDI;
  }
  
  @RequestMapping(params={"accion=selectCodTipPedi","codTipPedi", "desTipPedi"}) 
    protected @ResponseBody String[] selectCodTipPedi(@RequestParam("codTipPedi") String codTipPedi, @RequestParam("desTipPedi") String desTipPedi,
                                                      @ModelAttribute(FORM_KEY) SelecTipoPedidoForm form,HttpServletRequest request)throws Exception{        
        logger.info(this.getClass().toString()+" "+"metodo: selectCodTipPedi");        
        String[] chksTipPedi = (String[])request.getSession(false).getAttribute("chksTipPedi");
        if ((desTipPedi.contains("PREDICC") == true)){
            request.getSession(false).setAttribute("prediccion",true);
        }
        if(("").equals(codTipPedi))
            return chksTipPedi; 
        if(chksTipPedi!=null)
            for(int i=0;i<chksTipPedi.length;i++){                
                if(chksTipPedi[i]!=null&&chksTipPedi[i].equals(codTipPedi)){
                    break;
                }
                if(chksTipPedi[i]==null){
                    chksTipPedi[i] = codTipPedi;
                    break;
                }
            }            
        else{
            chksTipPedi= new String[1000];
            chksTipPedi[0]=codTipPedi;
        }        
        request.getSession(false).setAttribute("chksTipPedi",chksTipPedi);  
        return chksTipPedi;
    }
    
    @RequestMapping(params={"accion=deselectCodTipPedi","codTipPedi","desTipPedi"}) 
    protected @ResponseBody String[] deselectCodTipPedi(@RequestParam("codTipPedi") String codTipPedi,@RequestParam("desTipPedi") String desTipPedi,
                                                        @ModelAttribute(FORM_KEY) SelecTipoPedidoForm form,HttpServletRequest request)throws Exception{        
        logger.info(this.getClass().toString()+" "+"metodo: deselectCodTipPedi");        
        String[] chksTipPedi = (String[])request.getSession(false).getAttribute("chksTipPedi");
        if ((desTipPedi.contains("PREDICC") == true)){
            request.getSession(false).setAttribute("prediccion",false);
        }
        if(chksTipPedi!=null && chksTipPedi.length > 0)
            for(int i=0;i<chksTipPedi.length;i++){                
                if(chksTipPedi[i]!=null && chksTipPedi[i].equals(codTipPedi)){
                    chksTipPedi[i] = null;
                    break;
                }
            }            
        
        request.getSession(false).setAttribute("chksTipPedi",chksTipPedi);        
        return chksTipPedi;
    }        
  
  
}
